package com.pro.xwt.common.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pro.xwt.common.example.BProjectExample;
import com.pro.xwt.common.mapper.BProjectMapper;
import com.pro.xwt.common.mapper.BTaskMapper;
import com.pro.xwt.common.mapper.RProjEmpMapper;
import com.pro.xwt.common.mapper.RProjFlowMapper;
import com.pro.xwt.common.mapper.RTempFlowMapper;
import com.pro.xwt.common.model.BProject;
import com.pro.xwt.common.model.RProjEmp;
import com.pro.xwt.common.model.RProjFlow;
import com.pro.xwt.common.model.RTempFlow;
import com.pro.xwt.common.service.BProjectService;
import com.pro.xwt.common.utils.Common;

@Service("bProjectService")
public class BProjectServiceImpl extends AbstractService<BProject, String> implements BProjectService {
	@Autowired
	BProjectMapper bProjectMapper;
	@Autowired
	RTempFlowMapper rTempFlowMapper;
	@Autowired
	RProjEmpMapper rProjEmpMapper;
	@Autowired
	RProjFlowMapper rProjFlowMapper;
	@Autowired
	BTaskMapper bTaskMapper;

	public int insert(BProjectExample t) {

		return bProjectMapper.insert(t);
	}

	public List<BProject> queryListByPojo(BProjectExample t) {
		return bProjectMapper.queryListByPojo(t);
	}

	@Override
	public List<BProject> selectProjectByEmployee(String empId) {
		return bProjectMapper.selectProjectByEmployee(empId);
	}

	@Override
	public int insertProj(BProjectExample bProjectExample) {
		Integer flag = 0;
		bProjectExample.setId(Common.generateUUID());
		Date date = new Date();
		bProjectExample.setCreateTime(date);
		flag = bProjectMapper.insert(bProjectExample);
		List<RTempFlow> list = rTempFlowMapper.selectRTempFlowByBTemplate(bProjectExample.getTempTypeId());
		for (RTempFlow rtTempFlow : list) {
			RProjFlow rProjFlow = new RProjFlow();
			rProjFlow.setId(Common.generateUUID());
			rProjFlow.setProjId(bProjectExample.getId());
			rProjFlow.setName(rtTempFlow.getName());
			rProjFlow.setCreateTime(date);
			rProjFlow.setCreateEmpId(bProjectExample.getCreateEmpId());
			rProjFlow.setSort(rtTempFlow.getSort());
			flag = rProjFlowMapper.insert(rProjFlow);
		}
		//项目创建人插入项目成员
		RProjEmp rProjEmp1 = new RProjEmp();
		rProjEmp1.setDeleteState("0");
		rProjEmp1.setEmpId(bProjectExample.getCreateEmpId());
		rProjEmp1.setCreateTime(date);
		rProjEmp1.setProjId(bProjectExample.getId());
		rProjEmp1.setCreateEmpId(bProjectExample.getCreateEmpId());
		flag = rProjEmpMapper.insert(rProjEmp1);
		String[] string = bProjectExample.getEmpId();
		for (String id : string) {
			RProjEmp rProjEmp = new RProjEmp();
			rProjEmp.setDeleteState("0");
			rProjEmp.setEmpId(id);
			rProjEmp.setCreateTime(date);
			rProjEmp.setProjId(bProjectExample.getId());
			rProjEmp.setCreateEmpId(bProjectExample.getCreateEmpId());
			flag = rProjEmpMapper.insert(rProjEmp);
		}
		
		return flag;
	}
	@Override
	public Map<String, Object> differentStateProjectSum() {
		// TODO Auto-generated method stub
		return bProjectMapper.differentStateProjectSum();
	}

	@Override
	public int deleteProjById(String ProjId) {
		int flag = bProjectMapper.deleteById(ProjId);
		flag = bTaskMapper.deleteTasksByProjId(ProjId);
		flag = rProjFlowMapper.deleteFlowByProjId(ProjId);
		flag = rProjEmpMapper.deleteEmpByProjId(ProjId);
		return flag;
	}

	@Override
	public int update(BProjectExample t) {
		Map<String, Object> map = new HashMap<>();
		rProjEmpMapper.deleteEmpByProjId(t.getId());
		String[] string = t.getEmpId();
		for (String id : string) {
			RProjEmp rProjEmp = new RProjEmp();
			rProjEmp.setDeleteState("0");
			rProjEmp.setEmpId(id);
			rProjEmp.setCreateTime(new Date());
			rProjEmp.setProjId(t.getId());
			rProjEmp.setCreateEmpId(t.getCreateEmpId());
			rProjEmpMapper.insert(rProjEmp);
		}
		return bProjectMapper.update(t);
	}

}
